/** * Create by mzq * Description: 各城市每月佩戴用户增长 * Date: 2024/9/14 09:10 * Update:
2024/9/14 09:10 */
<template>
  <div class="UrbanWearGrowthRateBarBg">
    <div class="title">各城市每月佩戴用户</div>
    <div id="UrbanWearGrowthRateBar"></div>
  </div>
</template>
<script setup>
import * as echarts from 'echarts'
import { onMounted, ref } from 'vue'
import { getRegionalInfo } from '@/api/index.js'
import { cityArr } from '@/public/city.js'

// 城市arr
const cityList = ref([])

// 城市人员数据
const cityNumber = ref([])

// 获取接口数据
const getList = async () => {

  // 城市数据
  let reqArr = []
  for (let i = 0; i < cityArr.length; i++) {
    reqArr.push(cityArr[i].name)
  }


  let res = await getRegionalInfo({
    params: reqArr
  })


  let topArr = []
  for (let regionalKey in res.data.data.regional) {
    let obj = {}
    obj.name = regionalKey
    obj.userCount = res.data.data.regional[regionalKey].userCount
    topArr.push(obj)
  }


  topArr.sort(function (a, b){
    return b.userCount - a.userCount
  })

  let firstFiveItems = topArr.slice(0, 5);

  for (let i = 0; i < firstFiveItems.length; i++) {
    cityList.value.push(firstFiveItems[i].name)
    cityNumber.value.push(firstFiveItems[i].userCount)
  }


  init()
}

getList()

const init = () => {
  let myChart = echarts.init(document.getElementById('UrbanWearGrowthRateBar'))
  let options = {
    title: {
      text: '单位:个',
      left: 'right',
      textStyle: {
        color: '#64E3FF',
        fontSize: 8
      }
    },
    grid: {
      left: '2%',
      right: '0%',
      bottom: '0%',
      top: '26%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      axisLabel:{
        textStyle: {
          fontSize: 10
        },
      },
      axisLine: {
        show: false,
        lineStyle: {
          color: '#fff'
        }
      },
      data: cityList.value,
      axisTick: {
        show: false
      }
    },
    yAxis: {
      type: 'value',
      lineStyle: {
        color: '#fff'
      },
      axisLine: {
        show: false,
        lineStyle: {
          color: 'rgba(255,255,255,0.16)'
        }
      },
      // y轴横向 标线
      splitLine: {
        show: true,
        lineStyle: {
          color: 'rgba(255,255,255,0.16)'
        }
      }
    },
    series: [
      {
        data: cityNumber.value,
        type: 'bar',
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
            {
              offset: 0,
              color: '#015EEA'
            },
            {
              offset: 1,
              color: '#00C0FA'
            }
          ])
        }
      }
    ]
  }
  myChart.setOption(options, true)

  window.addEventListener('resize', function () {
    myChart.resize()
  })
}

</script>
<style scoped lang="scss">
.UrbanWearGrowthRateBarBg {
  width: 0.7865rem /* 151/192 */;
  height: 0.5677rem /* 109/192 */;
  background: url('@/image/bg5.png') no-repeat;
  background-size: 100% 100%;

  .title {
    margin-top: 0.0521rem /* 10/192 */;
    margin-left: 0.0625rem /* 12/192 */;
    color: white;
    font-size: 0.0417rem /* 8/192 */;
  }

  #UrbanWearGrowthRateBar {
    width: 0.7292rem /* 140/192 */;
    height: 0.4375rem /* 84/192 */;
  }
}
</style>
